package io.gitee.zhangbinhub.admin.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.mybatisflex.annotation.Id
import com.mybatisflex.annotation.RelationManyToMany
import com.mybatisflex.annotation.Table
import io.gitee.zhangbinhub.admin.base.BaseTreeEntity
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.noear.snack4.annotation.ONodeAttr

@Table("t_organization")
@ApiModel("机构信息")
data class Organization(
    @Id
    @ApiModelProperty(name = "机构ID")
    var id: String = "",
    @ApiModelProperty(name = "机构名称")
    var name: String = "",
    @ApiModelProperty(name = "机构区域")
    var area: String = "",
    @ApiModelProperty(name = "机构编码")
    var code: String = "",
    @ApiModelProperty(name = "序号")
    var sort: Int = 0,
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_user_organization_set", // 中间表
        joinSelfColumn = "organization_id",
        joinTargetColumn = "user_id"
    )
    var userSet: MutableList<User> = mutableListOf(),
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_user_organization_mng_set", // 中间表
        joinSelfColumn = "organization_id",
        joinTargetColumn = "user_id"
    )
    var adminUserSet: MutableList<User> = mutableListOf()
) : BaseTreeEntity<Organization>() {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val organization = other as Organization
        return EqualsBuilder()
            .appendSuper(super.equals(other))
            .append(id, organization.id)
            .append(name, organization.name)
            .append(area, organization.area)
            .append(code, organization.code)
            .append(sort, organization.sort)
            .append(parentId, organization.parentId)
            .isEquals
    }

    override fun hashCode(): Int =
        HashCodeBuilder(17, 37)
            .appendSuper(super.hashCode())
            .append(id)
            .append(name)
            .append(area)
            .append(code)
            .append(sort)
            .append(parentId)
            .toHashCode()

    override fun toString(): String =
        StringBuilder("Organization(")
            .append("id=$id")
            .append(",name=$name")
            .append(",area=$area")
            .append(",code=$code")
            .append(",sort=$sort")
            .append(",parentId=$parentId")
            .append(")")
            .toString()
}